<template>
  <div>
    <el-row>
      <el-col :span="12" style="text-align:left">
        <span style="font-family:Microsoft;font-size: 15px;font-weight: bold;">税务信息</span>
      </el-col>
      <el-col :span="12" style="text-align: right">
        <el-dropdown @command="handleCommand">
          <span><i class="joys-icon-fusionmenu"></i></span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="edit">编辑</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-col>
    </el-row>
    <el-form :model="form" inline style="background-color: #F4F8F9">
      <el-row>
        <el-col :md="8" :lg="6">
          <el-form-item label="纳税人识别号" style="text-align: left" prop="identificationNumber">
            <ft-input :editable="editable" v-model="form.identificationNumber"></ft-input>
          </el-form-item>
        </el-col>
        <el-col :md="8" :lg="6">
          <el-form-item label="注册地址" style="text-align: left" prop="registerAddress">
            <ft-input :editable="editable" v-model="form.registerAddress"></ft-input>
          </el-form-item>
        </el-col>
        <el-col :md="8" :lg="6">
          <el-form-item label="注册电话" style="text-align: left" prop="registerPhone">
            <ft-input :editable="editable" v-model="form.registerPhone"></ft-input>
          </el-form-item>
        </el-col>
        <el-col :md="8" :lg="6">
          <el-form-item label="银行账户" style="text-align: left" prop="bankId">
            <ft-bank-account-select :editable="editable" v-model="form.bankId"
                                    @change="accountChange"
                                    :org-id="form.orgId"></ft-bank-account-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :md="8" :lg="6">
          <el-form-item label="银行户名" style="text-align: left" prop="accountName">
            <span v-if="bankAccount">{{this.bankAccount.accountName}}</span>
          </el-form-item>
        </el-col>
        <el-col :md="8" :lg="6">
          <el-form-item label="开户银行" style="text-align: left" prop="accountBank">
            <span v-if="bankAccount">{{this.bankAccount.accountBank}}</span>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div style="text-align: right">
      <el-button @click="cancel" v-if="editable" size="small">取消</el-button>
      <el-button @click="save" v-if="editable" type="primary" size="small">保存</el-button>
    </div>
  </div>
</template>

<script>

  import FtOrg from "./FtOrg";
  import FtBankAccountSelect from "./FtBankAccountSelect";

  export default {
    name: "OrgTax",
    props: ['orgId'],
    components: {FtBankAccountSelect, FtOrg},
    data() {
      return {
        editable: true,
        form: {orgId: null},
        bankAccount: {}
      }
    },
    mounted() {
      this.load()
    },
    methods: {
      handleCommand(value) {
        this.editable = true;
      },
      cancel() {
        if (this.form.orgId) {
          this.$http.get('/joys-rest/joys-master-data/orgs/queryOrgInfo/' + this.form.orgId).then(resp => {
            this.editable = false;
            this.form = resp.body
          }, resp => {
            this.$message.error(resp.body.message)
          })
        } else {
          this.editable = false;
          this.form = {}
        }
      },
      save() {
        this.$http.post('/joys-rest/joys-master-data/orgs/save', this.form).then(resp => {
          this.editable = false
          this.form = resp.body
          this.$emit("refresh", {orgId: this.form.orgId, type: 'OrgTax'})
          this.$message.success('操作成功！')
        }, resp => {
          this.$message.error(resp.body.message)
        })
      },
      load() {
        if (this.orgId) {
          this.bankAccount = null
          this.form = {}
          this.$http.get('/joys-rest/joys-master-data/orgs/queryOrgInfo/' + this.orgId).then(resp => {
            this.form = resp.body
          }, resp => {
            this.$message.error(resp.body.message)
          })
        }
      },
      accountChange(value) {
        this.bankAccount = value
      }
    },
    watch: {
      orgId() {
        this.editable = false
        this.load()
      }
    }
  }
</script>

<style scoped>

</style>
